@extends('cms/system/admin/template/theme_default/common')
@section('page-body')
    <div class="layui-form" id="sms-platform-form" lay-filter="sms-platform-form" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">平台名称</label>
            <div class="layui-input-block">
                <input class="layui-input" type="text" name="name" value="{{$data['name'] ?? ''}}" placeholder="请输入名称" autocomplete="off" lay-verify="required" required />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平台类型</label>
            <div class="layui-input-block">
                <select name="seder" id="seder" lay-filter="seder">
                    @foreach($seder as $seder_k => $seder_v)
                        <option value="{{$seder_k}}">{{$seder_v['name']}}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">平台配置</label>
            <div class="layui-input-block">
                <fieldset class="layui-elem-field">
                    <legend>配置</legend>
                    <div class="layui-field-box layui-form-pane" id="platform-seder-config"></div>
                </fieldset>
            </div>
        </div>
        <div class="layui-form-item layui-hide">
            <input type="button" value="添加" id="submit-create" lay-submit lay-filter="submit-create" />
            <input type="button" value="更新" id="submit-update" lay-submit lay-filter="submit-update" />
        </div>
    </div>
    @verbatim
        <script type="text/html" id="platform-seder-config-template">
            {{# layui.each(d.config, function (config_k, config_v) { }}
            <div class="layui-form-item" pane="">
                <label class="layui-form-label">{{ config_v.name }}</label>
                <div class="layui-input-block">
                    <input class="layui-input" type="text" name="seder_config[{{ config_k }}]" value="{{ config_v.value }}" />
                </div>
            </div>
            {{# }); }}
        </script>
    @endverbatim
    <script type="text/javascript">
        layui.config({
            base: '/layui-admin/'
        }).extend({
            index: 'lib/index'
        }).use(['index'], function () {
            // 类型设置
            var platformSederSetting = @json($seder);
            // 平台类型
            var platformSederValue = layui.$('#seder').val();
            // 平台配置
            var platformSederConfig = platformSederSetting[platformSederValue];
            // 配置区域
            var platformSederConfigArea = layui.$('#platform-seder-config');
            // 配置模板
            var platformSederConfigTemplate = layui.$('#platform-seder-config-template').html();
            // 渲染数据
            layui.laytpl(platformSederConfigTemplate).render(platformSederConfig, function (html) {
                platformSederConfigArea.html(html);
                layui.form.render();
            });

            // 创建
            layui.form.on('submit(submit-create)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                // 当前iframe层的索引
                var layerIndex = parent.layer.getFrameIndex(window.name);
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/sms/admin_platform/create',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        console.log('提交结果：', result);
                        // 重载表格
                        parent.layui.table.reload('sms-platform-list');
                        // 关闭弹层
                        parent.layer.close(layerIndex);
                    }
                });
            });
            // 更新
            layui.form.on('submit(submit-update)', function (data) {
                // 获取提交的字段
                var formField = data.field;
                formField.content = layui.layedit.getContent(layuiEdit);
                console.log('表单字段：', formField);
                // 当前iframe层的索引
                var layerIndex = parent.layer.getFrameIndex(window.name);
                // ajax提交
                layui.$.ajax({
                    url: layui.setter.apiLink + '/sms/admin_platform/update?id={{$data['id'] ?? 0}}',
                    type: 'POST',
                    data: formField,
                    success: function (result) {
                        console.log('提交结果：', result);
                        // 重载表格
                        parent.layui.table.reload('sms-platform-list');
                        // 关闭弹层
                        parent.layer.close(layerIndex);
                    }
                });
            });
        });
    </script>
@endsection